package model.workerWithFile;

import java.text.SimpleDateFormat;
import java.util.ArrayList;

import model.textFileWorkerWrapper.TextFileWorkerWrapper;

import org.apache.log4j.Logger;

import entities.IGarage;
import entities.IMaster;
import entities.IOrder;

public class WriterInFile {

	private static Logger log = Logger.getLogger(WriterInFile.class);

	static void saveGarageList(ArrayList<IGarage> garageList,
			TextFileWorkerWrapper garageFile) {

		try {
			ArrayList<String> temp = new ArrayList<String>();
			for (IGarage garage : garageList) {
				temp.add("" + garage.getNumberGarage());
			}

			garageFile.writeToFile(temp);
		} catch (ExceptionInInitializerError e) {
			log.fatal("File not found");
		} catch (NullPointerException ne) {
			log.fatal("File not found");
		}
	}

	static void saveMasterList(ArrayList<IMaster> masterList,
			TextFileWorkerWrapper masterFile) {
		ArrayList<String> temp = new ArrayList<String>();

		for (IMaster master : masterList) {
			temp.add(master.getNameMaster());
		}
		masterFile.writeToFile(temp);

	}

	static void saveOrderList(ArrayList<IOrder> orderList,
			TextFileWorkerWrapper orderFile) {
		ArrayList<String> temp = new ArrayList<String>();
		SimpleDateFormat f = new SimpleDateFormat("dd.MM.yyyy");

		for (IOrder order : orderList) {
			temp.add(order.getNumberOrder() + " "
					+ f.format(order.getFillingDate().getTime()) + " "
					+ f.format(order.getDeadlineDate().getTime()) + " "
					+ f.format(order.getBeginingDate().getTime()) + " "
					+ order.getPrice() + " " + order.getOrderStatus() + " "
					+ order.getMaster().getNameMaster() + " "
					+ order.getGarage().getNumberGarage());
		}

		orderFile.writeToFile(temp);

	}

}
